package com.xujl.mvcbind.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

public class CustomEditText extends EditText {
    private InputListener mInputListener;
    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged (CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged (CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged (Editable s) {
            if (mInputListener != null) {
                mInputListener.onInput(s == null ? "" : s.toString());
            }
        }
    };

    public CustomEditText (Context context) {
        super(context);
        init(context);
    }

    public CustomEditText (Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomEditText (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init (Context context) {
        removeTextChangedListener(mTextWatcher);
        addTextChangedListener(mTextWatcher);
    }

    public void setInputListener (InputListener inputListener) {
        mInputListener = inputListener;
    }

    public interface InputListener {
        void onInput (String s);
    }
}
